import time

import websitebase


class btctrade(websitebase.websitebase):

    _url_ticker = "https://api.btctrade.com/api/ticker?coin=%s"

    def __init__(self):
        websitebase.websitebase.__init__(self)

    def getlast(self, symbol="btc"):
        if self._string_is_empty(symbol):
            print("btctrade.getlast:输入的参数不能为空")
            return
        jdata = self.ticker(symbol)
        return jdata['last']

    # symbol:"btc"/"ltc"/"eth"/"doge"/"ybc"
    def ticker(self, symbol="btc"):
        if self._string_is_empty(symbol):
            print("btctrade.ticker:输入的参数不能为空")
            return
        if symbol != "btc" and symbol != "ltc" and symbol != "eth" and symbol != "doge" and symbol != "ybc":
            print("btctrade.ticker:输入的参数不正确")
            return
        url = self._url_ticker % (str(symbol))
        return self._req_json(url)

if __name__ == "__main__":
    t = time.time()
    a = btctrade()
    # print(a.ticker())
    print(a.getlast())
    print(time.time()-t)



# url = "https://www.btctrade.cn/api/v1/ticker.do?symbol=btc_cny"
#
# re = urllib.request.urlopen(url)
# data = re.read().decode("utf-8")
# print(data)
# jdata = json.loads(data)
# print(jdata)
# print(jdata['ticker']['last'])
